package com.guodi.model.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.guodi.common.utils.AjaxResult;
import com.guodi.common.utils.UuidUtil;
import com.guodi.core.boot.ctrl.BaseController;
import com.guodi.model.entity.IdtMonitorFill;
import com.guodi.model.entity.IdtReportFile;
import com.guodi.model.entity.IdtReportFill;
import com.guodi.model.service.IIdtIndicatorsService;
import com.guodi.model.service.IIdtMonitorFillService;
import com.guodi.model.service.IIdtReportFileService;
import com.guodi.model.service.IIdtReportFillService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import javax.annotation.Resource;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletResponse;
import java.util.*;


/**
 * @描述：上报值关联附件表控制类
 * @作者: 代鹏刚
 * @日期: 2020-11-07
 */
@RestController
@RequestMapping("/IdtReportFile")
@Api(value = "API - IdtReportFileController", description = "上报值关联附件表")
public class IdtReportFileController extends BaseController {

    @Resource
    public IIdtReportFileService idtReportFileService;

    @Resource
    private IIdtReportFillService idtReportFillService;

    @Resource
    private IIdtMonitorFillService iIdtMonitorFillService;

    @Resource
    private IIdtIndicatorsService iIdtIndicatorsService;

    /**
    * @描述: 根据文件类型分类查询数据
    * @入参:
    * @出参:
    * @作者: 代鹏刚
    * @日期: 2020-11-07
    **/
    @PostMapping("/listByReportId")
    @ApiOperation(value = " 根据文件类型分类查询数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "reportId", value = "数据上报ID", required = true, dataType = "string", paramType = "query")
    })
    public AjaxResult listByReportId(String reportId){
        LambdaQueryWrapper<IdtReportFile> queryWrapper = new QueryWrapper().lambda();
        queryWrapper.eq(IdtReportFile::getReportId, reportId);
        List<IdtReportFile> idtReportFiles = idtReportFileService.list(queryWrapper);
        return new AjaxResult(idtReportFiles);
    }


    /**
     * @描述: 新增
     * @入参:
     * @出参:
     * @作者: 代鹏刚
     * @日期: 2020-11-07
     **/
    @PostMapping("/save")
    @ApiOperation(value = "新增")
    @ApiImplicitParam(name = "reportId", value = "数据上报ID", required = true, dataType = "string", paramType = "query")
    public AjaxResult save(@ApiIgnore IdtReportFile idtReportFile, @RequestParam(required = true,value = "file") MultipartFile file)throws Exception{
        idtReportFile.setId(UuidUtil.get32UUID());
        //ftp文件上传
        if(file != null){
            String fileName = file.getOriginalFilename();
            String fileFormat = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".") + 1);
//            String filePath = FastDFSUtil.upload(file.getBytes(), fileFormat);
            idtReportFile.setFileName(fileName);
//            idtReportFile.setFilePath(filePath);
            idtReportFile.setFileType(fileFormat);
        }
        prepareData(idtReportFile, true);
        idtReportFileService.save(idtReportFile);
        return new AjaxResult(idtReportFile);
    }

    /**
     * @描述: 根据主键删除
     * @入参: id
     * @出参:
     * @作者: 代鹏刚
     * @日期: 2020-11-07
     **/
    @PostMapping("/delete")
    @ApiOperation(value = "根据主键删除")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "string", paramType = "query")
    })
    public AjaxResult delete(String id){
        idtReportFileService.removeById(id);
        IdtReportFile idtReportFile = idtReportFileService.getById(id);

        if(StringUtils.isNotBlank(idtReportFile.getFilePath())){
            String group = idtReportFile.getFilePath().substring(0, idtReportFile.getFilePath().indexOf("/"));
//            FastDFSUtil.delete(group, idtReportFile.getFilePath());
        }
        return new AjaxResult();
    }

    /**
    * @描述: 根据reportId删除
    * @入参:
    * @出参:
    * @作者: 代鹏刚
    * @日期: 2020/11/7 18:24
    **/
    @PostMapping("/deleteByReportId")
    @ApiOperation(value = "根据reportId删除")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "reportId", value = "主键ID", required = true, dataType = "string", paramType = "query")
    })
    public AjaxResult deleteByReportId(String reportId){
        idtReportFileService.deleteByReportId(reportId);
        Map map = new HashMap();
        map.put("report_id",reportId);
        List<IdtReportFile> idtReportFiles = (List<IdtReportFile>)idtReportFileService.listByMap(map);
        if(idtReportFiles.size() > 0){
            for (IdtReportFile idtReportFile : idtReportFiles) {
                if (StringUtils.isNotBlank(idtReportFile.getFilePath())) {
                    String group = idtReportFile.getFilePath().substring(0, idtReportFile.getFilePath().indexOf("/"));
//                    FastDFSUtil.delete(group, idtReportFile.getFilePath());
                }
            }
        }
        return new AjaxResult();
    }

    /**
    * @描述: 文件批量下载
    * @入参:
    * @出参:
    * @作者: 代鹏刚
    * @日期: 2020/11/7 11:55
    **/
    @PostMapping("/downloadMulFile")
    @ApiOperation(value = "文件批量下载")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "reportId", value = "数据上报ID", required = true, dataType = "string", paramType = "query")
    })
    public void downloadMulFile(String reportId, HttpServletResponse response){
        Map map = new HashMap();
        map.put("report_id",reportId);
        List<IdtReportFile> idtReportFiles = (List<IdtReportFile>)idtReportFileService.listByMap(map);
        if(idtReportFiles.size() > 0){
            try {
                String downloadName = "fileList";
                String indicatorsId = "";
                IdtReportFill reportFill = idtReportFillService.getById(reportId);
                if (reportFill != null && StringUtils.isNotBlank(reportFill.getIndicatorsId())) {
                    indicatorsId = reportFill.getIndicatorsId();
                }
                IdtMonitorFill monitorFill = iIdtMonitorFillService.getById(reportId);
                if (monitorFill != null && StringUtils.isNotBlank(monitorFill.getIndicatorsId())) {
                    indicatorsId = monitorFill.getIndicatorsId();
                }
                if (StringUtils.isNotBlank(indicatorsId)) {
                    downloadName = iIdtIndicatorsService.getById(indicatorsId).getName() + "附件集合";
                }
//                List<FastDFSFileNodeDTO> fileNodeDTOs = new ArrayList<>();
//                for (IdtReportFile idtReportFile : idtReportFiles) {
//                    downloadName = idtReportFile.getFileName();
//                    if (StringUtils.isNotBlank(idtReportFile.getFilePath())) {
//                        FastDFSFileNodeDTO fastDFSFileNodeDTO = new FastDFSFileNodeDTO();
//                        fastDFSFileNodeDTO.setFileId(idtReportFile.getFilePath());
//                        fastDFSFileNodeDTO.setFileName(idtReportFile.getFileName());
//                        fileNodeDTOs.add(fastDFSFileNodeDTO);
//                    }
//                }
//                if(fileNodeDTOs.size() > 0){
//                    FastDFSUtil.download2zip(fileNodeDTOs, downloadName);
//                }
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }

    }

    /**
    * @描述: 单文件下载
    * @入参:
    * @出参:
    * @作者: 代鹏刚
    * @日期: 2020/11/7 16:38
    **/
    @PostMapping("/downloadFile")
    @ApiOperation(value = "单文件下载")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "path", value = "当前选中的附件路径", required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "当前选中附件名称", required = true, dataType = "string", paramType = "query")
    })
    public void downloadFile(String path,String name,HttpServletResponse response){
        String group = path.substring(0, path.indexOf("/"));
//        FastDFSUtil.downloadFile(group, path);
    }

    private void prepareData(IdtReportFile idtReportFile, boolean isSave) {
        if(isSave){
            idtReportFile.setCreator(this.getUser().getUserId().toString());
            idtReportFile.setCreateTime(new Date());
            //防止误修改
            idtReportFile.setModifier(null);
            idtReportFile.setModifyTime(null);
        }else{
            idtReportFile.setModifier(this.getUser().getUserId().toString());
            idtReportFile.setModifyTime(new Date());
            //防止误修改
            idtReportFile.setCreator(null);
            idtReportFile.setCreateTime(null);
        }
    }
}
